Example of the usage of the time and datetime modules in Python, pythondatetime
Time module method:Time. time (): Get the timestamp of the current time
Time. localtime (): receives a timestamp and converts it to a tuples of the current time. If no parameter is specified, time. time () is input as the parameter by default.
Time. localtime ()
format of the Time.asctime (), and by default the Time.time () is passed as a parameter.
Time.gmtime (): Converts a timestamp to utc+0 time zone (China should be + 8 time zone, 8 hours apart), and will default to Time.time () as a parameter.
Time.strftime (Format,time.localtime ()): Converts a time tuple to a formatted time character, without giving the time tuple argument the default to pass Time.localtime () as a parameter
For example, th
follows. Path: The Path to be determined. The following example checks whether E: \ book \ Temp is a file.
>>>importos
>>>os.path.isfile('E:\\book\\temp')
Determine whether it is a file
False
E: \ book \ Temp is not used to list all files in the directory.
Keywords:
dirimportstring,os,sys
dir='/var'
print'-----------nosubdir'
files=os.listdir(dir)
forfinfiles:
printdir+os.sep+f
print'-----------alldir'
forro
])%H: Hours (24-hour, [0, 23])%I: Hour (12-hour). [0, 11])%j: Number of days in the year [001,366] (the day of the current year)%m: Month ([01,12])%M: Minutes ([00,59])%p:am or PM%s: Seconds (the range is [00,61]. Why not [00, 59], reference Python manual ~_~)%u: Week in the Year of the Week of the year), Sunday as the first day of the week%w: Today in this week's days, the range is [0, 6],6 represents Sunday%W: Week of the Year (the Week of the year)
here corresponds to the structure time one by one behind it.Import Time Print (Time.strptime ("2018:06:09-15:21:36","%y:%m:%d-%x")) C:\python35\python3.exe D:/pyproject/day21 module/Timemodule. Pytime.struct_time (tm_year=2018, Tm_mon=6, TM _mday=9, tm_hour=15, tm_min=21, tm_sec=36, tm_wday=5, tm_yday=160, Tm_isdst=-1)7, Asctime (p_tuple=None) can be added to the structure of parameters, no parameter default is the current timeIf you don't have a custom demand time format, you can use this met
it yourself ~~
4. convert to a stringThe strftime () method is provided for datetime, date, and time. This method receives a format string and outputs a string representation of the date and time. The following table is pulled from the python manual. I have made some simple translations ~~).
Format characters and meanings
%. For example, Wednesday is Web% A full
Python processes the time and date modules, mainly using the datetime, time, and calendar modules. The following article mainly introduces the datetime module in Python. you can refer to it for reference. let's take a look at it. Preface
Python provides multiple built-in mo
The problem arises when I try to start by adding real time to my self-built web page.I've previously known that Python has a module datetime for events and dates. The following import datetime and experiment.>>> Import datetime>>> Type (datetime)class'module'>It is known tha
Time datetime and random in common Python modules, and time in python modules
This section outlines:
I. Module introduction:
Module, which implements a set of code for a specific function with a single worker code.
Similar to functional programming and process-oriented programming, functional programming completes a function. Other code can be called to provide
APR #%b month Full write. such as the string representation of the April #%c: DateTime in April. (For example: 04/07/10 10:43:39) #%d: Days in this month (is the day of the months) #%f: microseconds (range [0,999999]) #%H: Hours (24 hours, [0,]) #%I: Hours (12 hours, [0, 11] ) #%j: Days in the year [001,366] (is day of the year) #%m: Month ([01,12]) #%m: minutes ([00,59]) #%p:am or PM #%s: seconds (range i
just +8), the rest are the same.The following function can return a formatted datetime and look more intuitive.print(time.ctime())print(time.asctime())# 由于使用默认参数和上面的结果一样print(time.ctime(time.time()))print(time.asctime(time.localtime())) thu Jul 6 09:46:15 2017Thu Span class= "Hljs-selector-tag" >jul 6 09:46 : 2017thu jul 6 09 :46:15 2017 thu jul 6 09 : 46:15
ctime()You can pass in a timestamp, and the current timestamp is used as
the date and time class explained above. ):
Datetime.year, month, day, hour, minute, second, microsecond, Tzinfo:
Datetime.date (): Gets the Date object;
Datetime.time (): Gets the time object;
Datetime. Replace ([year[, month[, day[, hour[, minute[, second[, microsecond[, Tzinfo]] []]]:
Datetime. Timetuple ()
Datetime. Utctimetuple ()
This article mainly introduces the usage examples of the time module and datetime module in Python. it mainly demonstrates the printing and calculation of time and date. For more information, see
Time Module method:
Time. time (): Get the timestamp of the current time
Time. localtime (): Receives a timestamp and converts it to a tuples of the current time. If no parameter is specified, time. time () is in
Python provides multiple built-in modules for date and time operations, such as calendar, time, and datetime. Today, we will mainly discuss how to use datetime. if you need it, you can refer to it. Common time conversion and processing functions:
Import datetime # obtain the current time d1 =
Common datetime time processing methods in Python
Python provides multiple built-in modules for date and time operations, such as calendar, time, and datetime. Today, we will mainly discuss how to use datetime. If you need it, you can refer to it.
Common time conversion an
Python time module datetime, time, calendar usage, pythondatetime
This article briefly summarizes the Python processing time and date modules, mainly the use of datetime, time, and calendar modules. I hope this article will be helpful to anyone who has learned Python.
The fi
())#the total number of seconds of the time differencePrint(d_timedelta.microseconds)#the number of milliseconds (not the total) of the time differencePrint(d_timedelta.days)#total days of the time difference (rounded)" "Relationship Transformation" "#Convert a Datetime.datetime object to a time stringD_datetime_str = D.strftime ('%y-%m-%d%h:%m:%s')Print(D_DATETIME_STR)" "%a Week's shorthand. As Wednesday for Web%a week's full write. For example, Wed
April, the string representation of April #%c: DateTime. (Example: 04/07/10 10:43:39) #%d: Days in this month (the day of the Week) #%f: microseconds (range [0,999999]) #%H: Hours (24-hour, [0]) #%I: Hours (12-hour, [0, ] #%j: Number of days in the year [001,366] (the day of the current year) #%m: Month ([01,12]) #%m: minutes ([00,59]) #%p:am or PM #%s: seconds (range = [00,61], why not [00, 59], refer to
Commonly used datetime time processing method in Python, pythondatetime
Common time conversion and processing functions:
Import datetime # obtain the current time d1 = datetime. datetime. now () print d1 # The current time plus half an hour d2 = d1 +
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.